﻿namespace NTLib.Core.UTest.Configuration.Attributes
{
    using Core.Configuration;
    using Core.Configuration.Attributes;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Test.Resources;
    using NTLib.Core.Reflection;
    using DataSample;
    using Core.Configuration.Values;

    [TestClass]
    public class ProviderConfigurationAttrTest : BaseTestClass
    {
        #region Class

        [ProviderConfiguration(typeof(ToBeConfigNotIconfig))]
        private class ToBeConfigNotIconfig
        {
        }

        [ProviderConfiguration(null)]
        private class ToBeConfigNULL
        {
        }

        [ProviderConfiguration(typeof(TestConfigurationProvider))]
        private class TobeConfigProvider : ToBeConfigured
        {

        }

        #endregion

        #region Methods

        /// <summary>
        /// Check if the type pass as configuration provider is not null and implement <see cref="IConfigurationProvider"/>
        /// </summary>
        [TestCategory(ConfigManagerUTest.CONFIG_CATEGORIE)]
        [TestMethod]
        public void Config_ProviderAttr_TypeCheck()
        {
            try
            {
                var localVar = new ToBeConfigNULL();
                Assert.Fail("An exception ArgumentNullException must be raised");
            }
            catch (Exception ex)
            {
                Assert.IsTrue(base.ExceptionTest<ArgumentNullException>(ex));
            }

            try
            {
                var localVar = new ToBeConfigNotIconfig();
                Assert.Fail("An exception ArgumentException must be raised because ToBeConfigNotIconfig doesn't implement IConfigurationProvider");
            }
            catch (Exception ex)
            {
                Assert.IsTrue(base.ExceptionTest<ArgumentException>(ex));
            }
        }

        /// <summary>
        /// Check that the attribute relay correclty the information between the provider and the instance.
        /// </summary>
        [TestCategory(ConfigManagerUTest.CONFIG_CATEGORIE)]
        [TestMethod]
        public void Config_ProviderAttr_Relay()
        {
            var testKey = "testOrigin";
            ConfigurationCollection collection = new ConfigurationCollection();
            StringConfig config = new StringConfig(testKey, "valueOrigin");

            ToBeConfigured inst = new TobeConfigProvider();

            var configurator = inst.GetMandatoryAttribute(typeof(ProviderConfigurationAttribute)) as ProviderConfigurationAttribute;

            Assert.IsNotNull(configurator);

            var result = configurator.CollectConfigurationValue(typeof(TobeConfigProvider), config);

            Assert.AreEqual(result.Count, 2);
            Assert.IsTrue(result.ContainsKey(testKey));
            Assert.IsTrue(result.ContainsKey(TestConfigurationProvider.TestKey));
        }

        #endregion
    }
}